<?php
/**
 * @copyright: ©2024 永州 微聚商网络科技有限公司
 * @link: https://www.wEijuyunke.com
 */

namespace app\mobile\controller;

use app\admin\model\FigureAvatar;
use app\admin\model\FigureCompositeAvatar;
use app\admin\model\FigureResourcesGoods;
use app\admin\model\FigureUser;
use app\admin\model\FigureVoiceTwin;
use app\BaseController;
use think\App;

/**
 * 资产
 *
 * @author Administrator
 *
 */
class ResourcesGoods extends BaseController
{

    /**
     * 敏感词校验
     * http://aigc.weijuyunke.cn/mobile/resourcesGoods/releaseGood
     *
     * @param
     *            uid 用户id
     * @param
     *            title 标题
     * @param
     *            type 1 高级形象  2 标准形象 3 声音
     * @param
     *            price 价格
     */
    public function releaseGood()
    {
        try {

            $uid = input('uid');
            $user = FigureUser::where('id', $uid)->find();
            if (empty($user)) {
                return result(-1, '用户不存在');
            }
            $title = input('title');
           /* $inputPrice = input('price');
            foreach ($inputPrice as $value){
                $price[$value['index']]= $value['price'];
            }*/
            $price = json_encode(input('price'));
            $relation_id = input('relation_id');
            $type = input('type');
            $figureAvatarId = 0;
            $compositeFigureAvatarId = 0;
            $figureVoiceTwinId = 0;
            $FigureResourcesGoodsModel = new FigureResourcesGoods();

            if ($type == 1) {
                $figureAvatarId = $relation_id;
                $model  =  $avatarModel = new FigureAvatar();
                $avatarInfo = $avatarModel->where(['id' => $relation_id, 'uid' => $uid])->find();
                if (empty($avatarInfo)) {
                    return result(-1, '请选择自己的高级形象');
                }
                if ($avatarInfo['order_id']){
                    return result(-1, '此形象为您购买的形象，不能售出');
                }
                $figureResourcesGoodsInfo = $FigureResourcesGoodsModel->where(['figure_avatar_id' => $figureAvatarId, 'type' => $type])->find();
                if ($figureResourcesGoodsInfo) {
                    if (empty($figureResourcesGoodsInfo['delete_time']) && $figureResourcesGoodsInfo['status'] == 1) {
                        return result(-1, '此形象已上架');
                    }
                    if (empty($figureResourcesGoodsInfo['delete_time']) && $figureResourcesGoodsInfo['status'] == 1) {
                        return result(-1, '此形象已下架');
                    }
                }

            } else {
                $figureVoiceTwinId = $relation_id;
                $model  =   $figureVoiceTwinModel = new FigureVoiceTwin();
                $figureVoiceTwinInfo = $figureVoiceTwinModel->where(['id' => $relation_id, 'uid' => $uid])->find();
                if (empty($figureVoiceTwinInfo)) {
                    return result(-1, '请选择自己的声音');
                }
                if ($figureVoiceTwinInfo['order_id']){
                    return result(-1, '此形象为您购买的形象，不能售出');
                }
                if ($figureVoiceTwinInfo['is_status'] != 2) {
                    return result(-1, '声音为审核通过');
                }
                $figureResourcesGoodsInfo = $FigureResourcesGoodsModel->where(['figure_voice_twin_id' => $figureVoiceTwinId, 'type' => $type])->find();
                if ($figureResourcesGoodsInfo) {
                    if (empty($figureResourcesGoodsInfo['delete_time']) && $figureResourcesGoodsInfo['status'] == 1) {
                        return result(-1, '此声音已上架');
                    }
                    if (empty($figureResourcesGoodsInfo['delete_time']) && $figureResourcesGoodsInfo['status'] == 1) {
                        return result(-1, '此声音已下架');
                    }
                }
            }

            $data = [
                'figure_avatar_id' => $figureAvatarId,
                'composite_figure_avatar_id' => $compositeFigureAvatarId,
                'figure_voice_twin_id' => $figureVoiceTwinId,
                'title' => $title,
                'price' => $price,
                'type' => $type,
                'uid' => $uid,
                'status' => 1,
                'create_time' => time()
            ];
            $FigureResourcesGoodsModel->insert($data);
            $model->where(['id' =>$relation_id])->update(['is_share' =>2]);
            return result(0, '添加成功');
        } catch (\Exception $e) {
            return result(-1, '异常' . $e->getLine() . "--->" . $e->getMessage());
        }
    }


    /**
     * 敏感词校验
     * http://aigc.weijuyunke.cn/mobile/resourcesGoods/goodsList
     *
     * @param
     *            uid 用户id
     * @param
     *            type 1 高级形象  2 标准形象 3 声音
     * @param
     *            status 1 上架  2 下架
     */
    public function goodsList()
    {
        $request = $this->request;
        $param = $request->param();
        try {
            $where[] = ['fa.delete_time', '=', null];
            $where[] = ['fca.delete_time', '=', null];
            $where[] = ['fat.delete_time', '=', null];
            $order = 'create_time';
            if (!empty($param['uid'])) {
                $where[] = ['frg.uid', '=', $param['uid']];
            }
            if (isset($param['type']) && !empty($param['type'])) {
                if ($param['type'] == 1) {
                    $where[] = ['frg.type', '=', $param['type']];
                } elseif ($param['type'] == 2) {
                    $where[] = ['frg.type', '=', $param['type']];
                } elseif ($param['type'] == 3) {
                    $where[] = ['frg.type', '=', $param['type']];
                } elseif ($param['type'] == 4) { // 标准形象和高级形象
                    $where[] = ['frg.type', 'in', [1, 2]];
                }

            }
            if (!empty($param['status'])) {
                $where[] = ['frg.status', '=', $param['status']];
            }
            if (!empty($param['order_field'])) {
                $order = $param['order_field'];
            }
            $pindex = max(1, intval(input('page')));
            $psize = empty($param['psize']) ? 10 : intval($param['psize']);
            $figureResourcesGoods = new FigureResourcesGoods();
            $total = $figureResourcesGoods->alias("frg")
                ->leftJoin("figure_user fu", "frg.uid = fu.id")
                ->leftJoin("figure_avatar fa", "frg.figure_avatar_id = fa.id")
                ->leftJoin("figure_composite_avatar fca", "frg.composite_figure_avatar_id = fca.id")
                ->leftJoin("figure_voice_twin fat", "frg.figure_voice_twin_id = fat.id")
                ->where($where)->count();

            if ($total) {
                $list = $figureResourcesGoods->alias("frg")
                    ->leftJoin("figure_user fu", "frg.uid = fu.id")
                    ->leftJoin("figure_avatar fa", "frg.figure_avatar_id = fa.id")
                    ->leftJoin("figure_composite_avatar fca", "frg.composite_figure_avatar_id = fca.id")
                    ->leftJoin("figure_voice_twin fat", "frg.figure_voice_twin_id = fat.id")
                    ->field('frg.*,
                    fu.id as fu_id,fu.avatar as fu_avatar,fu.nickname as fu_nickname,
                    fa.id as fa_id,fa.name as fa_name,fa.type as  fa_type,fa.video_url as fa_video_url , fa.video_cover  as fa_video_cover,fa.image_url as fa_image_url ,fa.video_backgroud_image as fa_video_backgroud_image ,fa.video_backgroud_color as fa_video_backgroud_color ,    
                    fca.id as fca_id,fca.name as fca_name,fca.url as fca_url,
                    fat.id as fat_id, fat.name as fat_name,fat.voice_urls as fat_voice_urls,fat.train_mode as fat_train_mode,fat.media_url as fat_media_url')
                    ->where($where)
                    ->order($order, 'desc')
                    ->limit(($pindex - 1) * $psize, $psize)
                    ->select();
                if ($list){
                    foreach ($list as $key => $value){
                        $list[$key]['price'] = json_decode($value['price']);
                    }
                }
            } else {
                $list = array();
            }
            $totalPage = ceil($total / $psize);
            return result(0, 'success', array(
                'list' => $list,
                'total' => $total,
                'pindex' => $pindex,
                'psize' => $psize,
                'totalPage' => $totalPage
            ));
        } catch (\Exception $e) {
            return result(-1, '异常' . $e->getLine() . "--->" . $e->getMessage());
        }
    }


    public function updateStatus()
    {

        $request = $this->request;
        $param = $request->param();
        try {
            $uid = $param['uid'];
            $user = FigureUser::where('id', $uid)->find();
            if (empty($user)) {
                return result(-1, '请操作自己的数据');
            }
            $figureResourcesGoods = new FigureResourcesGoods();
            $figureResourcesGoods->where(['uid' => $uid, 'id' => $param['id']])->update(['status' => $param['status']]);
            return result(0, '操作成功');
        } catch (\Exception $e) {
            return result(-1, '异常' . $e->getLine() . "--->" . $e->getMessage());
        }
    }

    public function editGoods()
    {
        try {
            $request = $this->request;
            $param = $request->param();
            $user = FigureUser::where('id', $param['uid'])->find();
            if (empty($user)) {
                return result(-1, '请操作自己的数据');
            }
            $data = [
                'title' => $param['title'],
                'price' => json_encode($param['price']),
            ];
            $figureResourcesGoods = new FigureResourcesGoods();
            $figureResourcesGoods->where(['id' => $param['id']])->update($data);
            return result(0, '操作成功');
        } catch (\Exception $e) {
            return result(-1, '异常' . $e->getLine() . "--->" . $e->getMessage());
        }
    }

    public function releaseConfig()
    {
        $release = $release = sysconfig('release');
        return result(0, '操作成功', $release);
    }


    public function goodsDelete()
    {

        try {
            $uid = input('uid');
            $goodsId= input('goods_id');

            $user = FigureUser::where('id', $uid)->find();
            if (empty($user)) {
                return result(- 1, '用户不存在');
            }
            $figureResourcesGoodsModel = new FigureResourcesGoods();
            $figureResourcesGoods = $figureResourcesGoodsModel->where([
                'uid' => $uid,
                'id' => $goodsId
            ])->find();
            if (empty($figureResourcesGoods)) {
                return result(- 1, '数据不存在');
            }
            if ($figureResourcesGoods['type'] == 1){
                $model  =    new FigureAvatar();
                $model->where(['id' => $figureResourcesGoods['figure_avatar_id']])->update(['is_share' => 1]);
            }elseif ($figureResourcesGoods['type'] == 2){
                $model  =    new FigureCompositeAvatar();
                $model->where(['id' => $figureResourcesGoods['composite_figure_avatar_id']])->update(['is_share' => 1]);
            }else{
                $model   = new FigureVoiceTwin();
                $model->where(['id' => $figureResourcesGoods['figure_voice_twin_id']])->update(['is_share' => 1]);

            }

            $figureResourcesGoodsModel->where([
                'uid' => $uid,
                'id' => $goodsId
            ])->find()->delete();
            return result(0, '删除成功');
        } catch (\Exception $e) {
            return result(- 1, '异常' . $e->getLine() . "--->" . $e->getMessage());
            exit();
        }
    }
    
    public function getTime(){
        $data = getResourcesTime();
        return result(0, 'success',   $data);
    }



}